package com.eastfair.projectcore.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.projectcore.dto.*;
import com.eastfair.projectcore.entity.AppMenu;
import com.eastfair.projectcore.entity.Menu;
import com.eastfair.projectcore.service.AppMenuService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.eastfair.boot.controller.SuperController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.echo.core.EchoService;
import com.eastfair.core.base.R;
import com.eastfair.projectcore.vo.AppMenuVO;
import com.eastfair.projectcore.vo.MenuVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 前端控制器
 * 前端菜单（之所以分开是因为避免指定上级菜单出错）
 * </p>
 *
 * @author ligang
 * @date 2021-10-28
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/appMenu")
@Api(value = "AppMenu", tags = "前端菜单（之所以分开是因为避免指定上级菜单出错）")
public class AppMenuController extends SuperController<AppMenuService, Long, AppMenu, AppMenuPageQuery, AppMenuSaveDTO, AppMenuUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Override
    public void handlerResult(IPage<AppMenu> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<AppMenu> appMenuList = list.stream().map((map) -> {
            AppMenu appMenu = AppMenu.builder().build();
            //TODO 请在这里完成转换
            return appMenu;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(appMenuList));
    }

    /**
     * 删除,并删除旗下所有子菜单
     * @param ids 集合
     */
    @ApiOperation(value = "删除", notes = "删除")
    @DeleteMapping("/del")
    public R del(@RequestBody List<Long> ids) {
        if(ids!=null && !ids.isEmpty()){
            for(Long id : ids){
                baseService.del(id);
            }
        }
        return R.success();
    }
    /**
     * 查询前端菜单信息和全部子信息
     * @param id  前端菜单id
     * @return  查询结果
     */
    @ApiOperation(value = "查询前端菜单信息和全部子信息", notes = "查询前端菜单信息和全部子信息")
    @GetMapping("/findAllById")
    public R<AppMenuVO> findAllById(Long id) {
        return success(baseService.findAllById(id));
    }

    /**
     * 创建前端菜单节点
     * @param appMenuDTO 前端菜单实体
     */
    @ApiOperation(value = "创建前端菜单节点", notes = "创建前端菜单节点")
    @PostMapping("/createMenu")
    public R createMenu(@RequestBody @Validated({AppMenuDTO.createMenu.class}) AppMenuDTO appMenuDTO) {
        AppMenuSaveDTO appMenuSaveDTO = new AppMenuSaveDTO();
        BeanUtils.copyProperties(appMenuDTO, appMenuSaveDTO);
        return baseService.createMenu(appMenuSaveDTO);
    }

    /**
     * 修改菜单信息
     * @param appMenuUpdateDTO 前端菜单更新实体
     *
     */
    @ApiOperation(value = "修改菜单信息", notes = "修改菜单信息")
    @PutMapping("/updMenu")
    public R updMenu(@RequestBody AppMenuUpdateDTO appMenuUpdateDTO) {
        return baseService.updateMenu(appMenuUpdateDTO);
    }


    /**
     * 置顶
     * @param id 菜单id
     */
    @ApiOperation(value = "置顶", notes = "置顶")
    @PostMapping("/sortTop")
    public R sortTop(@RequestBody Long id) {
        baseService.sortTop(id);
        return R.success();
    }


    /**
     * 查询菜单全部子信息
     * @param id 菜单id
     * @return 查询结果
     */
    @ApiOperation(value = "查询菜单全部子信息", notes = "查询菜单全部子信息")
    @GetMapping("/findAllChildOfparentId")
    public R<List<AppMenuVO>> findAllChildOfparentId(Long id) {
        AppMenuVO appMenuVO = baseService.findAllById(id);
        return success(appMenuVO.getChildVoList());
    }




    /**
     * 查询菜单信息和全部子信息
     * @param appMenuDTO 前端菜单实体
     * @return 查询结果
     */
    @ApiOperation(value = "通过子菜单查询菜单信息和全部子信息", notes = "通过子菜单查询菜单信息和全部子信息")
    @PostMapping("/findAllByChildId")
    public R<List<AppMenuVO>> findAllByChildId(@RequestBody AppMenuDTO appMenuDTO) {
        List<Long> ids = new ArrayList<>();
        if (appMenuDTO != null && appMenuDTO.getMenuIds() != null) {
            ids = appMenuDTO.getMenuIds();
        }
        if(ids==null || ids.isEmpty()){
            //查顶级菜单
            QueryWrapper<AppMenu> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(AppMenu::getIsDeleted, BusinessConstant.DELETE_NO);
            queryWrapper.lambda().eq(AppMenu::getParentId, BusinessConstant.parentId);
            //查询用户
            List<AppMenu> menuList = baseService.list(queryWrapper);
            if (menuList != null && !menuList.isEmpty()) {
                ids.add(menuList.get(0).getId());
            }
        }
        return success(baseService.findAllByChildId(ids));
    }

    /**
     * 查询顶级下的菜单信息和全部子信息
     * @return 查询结果
     */
    @ApiOperation(value = "查询顶级下的菜单信息和全部子信息", notes = "查询顶级下的菜单信息和全部子信息")
    @GetMapping("/findAllOfTop")
    public R<AppMenuVO> findAllOfTop() {
        QueryWrapper<AppMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(AppMenu::getParentId, BusinessConstant.parentId);
        queryWrapper.lambda().eq(AppMenu::getIsDeleted, BusinessConstant.DELETE_NO);
        List<AppMenu> list=  baseService.list(queryWrapper);
        if(list!=null && !list.isEmpty()){
            List<Long> ids =list.stream().map(AppMenu::getId).collect(Collectors.toList());
            return success(baseService.findAllByIdNoButton(ids.get(0)));
        }
        return success(null);
    }

    /**
     * 查询菜单的权限标识
     * @param appMenuDTO 前端菜单实体
     * @return 查询结果
     */
    @ApiOperation(value = "查询菜单的权限标识", notes = "查询菜单的权限标识")
    @PostMapping("/findPermissionAllByChildId")
    public R<List<String>> findPermissionAllByChildId(@RequestBody AppMenuDTO appMenuDTO) {
        List<Long> ids = new ArrayList<>();
        if (appMenuDTO != null && appMenuDTO.getMenuIds() != null) {
            ids = appMenuDTO.getMenuIds();
        }
        return success(baseService.findPermissionAllByChildId(ids));
    }





    /**
     * 查询左侧菜单信息(包含菜单，目录)
     * @param id  菜单id
     * @return  查询结果
     */
    @ApiOperation(value = "查询菜单信息和全部子信息", notes = "查询菜单信息和全部子信息")
    @GetMapping("/findAllByIdNoButton")
    public R<AppMenuVO> findAllByIdNoButton(Long id) {
        return success(baseService.findAllByIdNoButton(id));
    }



}
